/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package base;

import java.util.Date;

/**
 *  Performs the calculations for finding the BMR,BMI,allowed range of weight for
 *  user considering a 3 week period, and the calorie consumed assuming the table
 *  has calorie/unit gm and calorie/unit ml.
 * @author MEERA
 */
public class Calculator {
    /*
     * Assuming table has calorie/unit gm and calorie/unit ml
     */

    public static float getCalorie(String qnty, String unit, String perUnitCal) {
        float cal = 0;
        int unitConvF = 1;
        if (unit.equalsIgnoreCase("kg") || unit.equalsIgnoreCase("ltr")) {
            unitConvF = 1000;
        }
        cal = ((Float.parseFloat(qnty)) * (unitConvF) * (Float.parseFloat(perUnitCal))) / 1000;
        return cal;
    }

    public static float getBMI(float height, float weight, String htUnit, String wtUnit) {
        float calcBMI = 0;
        double calcWt = 0;
        double calcHt = 0;

        if (wtUnit.equalsIgnoreCase("pound")) {
            calcWt = weight * 0.45359237;
        } else {
            calcWt = weight;
        }
        if (htUnit.equalsIgnoreCase("ft")) {
            calcHt = height * 0.3048;

        } else {
            calcHt = height * 0.01;
        }

        calcBMI = (float) (calcWt / (calcHt * calcHt));
        return calcBMI;
    }

    public static float[] getWtRange(float height, String htUnit) {

        float wtRange[] = new float[2];

        double calcHt = 0;
        if (htUnit.equalsIgnoreCase("ft")) {
            calcHt = height * 0.3048;

        } else {
            calcHt = height * 0.01;
        }
        wtRange[0] = (float) (18.5 * calcHt * calcHt);
        wtRange[1] = (float) (24.9 * calcHt * calcHt);

        return wtRange;
    }

    public static float getBMR(boolean sex, float height, float weight, Date dob) {

        java.util.Calendar cal = java.util.Calendar.getInstance();
        cal.setTime(new Date());
        int thisYear = cal.get(java.util.Calendar.YEAR);
        cal.setTime(dob);
        int bornYear = cal.get(java.util.Calendar.YEAR);


        int age = thisYear - bornYear;
        if (sex == Sex.Female) {
            return (float) (655 + (9.6 * weight) + (1.8 * height) - (4.7 * age));
        } else {
            return (float) (66 + (13.7 * weight) + (5 * height) - (6.8 * age));
        }
    }
}
